package com.example.demo.controller;

import com.alibaba.fastjson.JSONObject;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.List;

/**
 * todo
 *
 * @author binYan
 * @date 2023-07-06 9:45
 **/
@RestController
@RequestMapping("/VIID")
public class ViidMotorController {
    Logger LOGGER = LogManager.getLogger(this);

    @RequestMapping(value = "/MotorVehicles", method = RequestMethod.POST, produces = "application/VIID+JSON;charset=utf-8")
    public GATResponseVO MotorVehicles(@RequestBody MotorVehicleDTO dto, HttpServletRequest request) {
        GATResponseVO vo = new GATResponseVO();
        FileOutputStream fos = null;
        try {
            //如果目录不存在，自动创建文件夹
            File dir = new File("E:\\img" );
            if (!dir.exists()) {
                dir.mkdirs();
            }
            //base64 图片过大
            List<SubImageListDTO.SubImageInfoObjectDTO> imageList = dto.getMotorVehicleListObject().getMotorVehicleObject().get(0).getSubImageList().getSubImageInfoObject();
            for (SubImageListDTO.SubImageInfoObjectDTO infoObjectDTO :imageList) {

                String image2 = infoObjectDTO.getData();
                infoObjectDTO.setData("base64图片已经替换");
                Base64.Decoder decoder = Base64.getDecoder();
                byte[] bytes = decoder.decode(image2);
                fos = new FileOutputStream( "E:\\img\\" +System.currentTimeMillis() + ".jpg");
                fos.write(bytes);
            }


            dto.getMotorVehicleListObject().getMotorVehicleObject().get(0).getSubImageList().getSubImageInfoObject().get(0).setData("base64图片已经替换");
            LOGGER.info("机动车上报的信息：{}", JSONObject.toJSONString(dto));
            String requestURI = request.getRequestURI();

            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            ResponseStatusObjectDTO objectDTO = new ResponseStatusObjectDTO();
            objectDTO.setId(dto.getMotorVehicleListObject().getMotorVehicleObject().get(0).getDeviceID());
            objectDTO.setLocalTime(sdf.format(System.currentTimeMillis()));
            objectDTO.setRequestURL(requestURI);
            objectDTO.setStatusCode(0);
            objectDTO.setStatusString("OK");
            vo.setResponseStatusObject(objectDTO);
        } catch (Exception e) {
            LOGGER.error("注销错误{}", e);
        }finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    LOGGER.error("相机上报过车抓拍出现错误:{}",e);
                }
            }
        }
        return vo;
    }

    @RequestMapping(value = "/NonMotorVehicles", method = RequestMethod.POST, produces = "application/VIID+JSON;charset=utf-8")
    public GATResponseVO NonMotorVehicles(@RequestBody NonMotorVehicleDTO dto, HttpServletRequest request) {
        GATResponseVO vo = new GATResponseVO();
        FileOutputStream fos = null;
        try {
            //base64 图片过大

            List<SubImageListDTO.SubImageInfoObjectDTO> imageList = dto.getNonMotorVehicleListObject().getNonMotorVehicleObject().get(0).getSubImageList().getSubImageInfoObject();
            for (SubImageListDTO.SubImageInfoObjectDTO infoObjectDTO :imageList) {

                String image2 = infoObjectDTO.getData();
                infoObjectDTO.setData("base64图片已经替换");
                Base64.Decoder decoder = Base64.getDecoder();
                byte[] bytes = decoder.decode(image2);
                fos = new FileOutputStream( "E:\\img\\" +System.currentTimeMillis() + ".jpg");
                fos.write(bytes);
            }
             LOGGER.info("非机动车上报的信息：{}", JSONObject.toJSONString(dto));
            String requestURI = request.getRequestURI();

            //如果目录不存在，自动创建文件夹
            File dir = new File("E:\\img" );
            if (!dir.exists()) {
                dir.mkdirs();
            }

            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            ResponseStatusObjectDTO objectDTO = new ResponseStatusObjectDTO();
            objectDTO.setId(dto.getNonMotorVehicleListObject().getNonMotorVehicleObject().get(0).getDeviceID());
            objectDTO.setLocalTime(sdf.format(System.currentTimeMillis()));
            objectDTO.setRequestURL(requestURI);
            objectDTO.setStatusCode(0);
            objectDTO.setStatusString("OK");
            vo.setResponseStatusObject(objectDTO);
        } catch (Exception e) {
            LOGGER.error("注销错误{}", e);
        }finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    LOGGER.error("相机上报过车抓拍出现错误:{}",e);
                }
            }
        }
        return vo;
    }
}
